package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ghost on 2014/10/10 0010.
 */
public class Pascals_Triangle_II {
    public List<Integer> getRow(int rowIndex) {
        int[] tmpRst = new int[rowIndex+1];
        List<Integer> list = new ArrayList<Integer>();
        tmpRst[0] = 1;
        if(rowIndex==0){
            list.add(1);
            return list;
        }
        for(int i=1;i<=rowIndex;i++){
            for(int j=-1;j<i;j++){
                if(j==-1){
                    int a = 0;
                    int b = tmpRst[0];
                    list.clear();
                    list.add(a+b);
                }
                if(j+1==i){
                    int a = tmpRst[j];
                    int b = 0;
                    list.add(a+b);
                }
                if(j!=-1 && j!=i-1){
                    int a = tmpRst[j];
                    int b = tmpRst[j+1];
                    list.add(a+b);
                }
            }
            for(int j=0;j<=i;j++){
                tmpRst[j] = list.get(j);
            }
        }
        list.clear();
        for(int i=0;i<=rowIndex;i++)
            list.add(tmpRst[i]);
        return list;
    }
    public static void main(String[] args){
        Pascals_Triangle_II pascals_triangle_ii = new Pascals_Triangle_II();
        List<Integer> list = pascals_triangle_ii.getRow(12);
        for(Integer integer: list)
            System.out.println(integer+" ");
    }
}
